package com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.widget

import android.content.Context
import android.view.View
import android.view.animation.Animation
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.vitabuybuy.tv.R
import com.library.common.base.BindRecycAdapter
import com.library.common.util.DensityUtil
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.databinding.ItemLiveGoodsBinding
import com.vitabuybuy.tv.databinding.ItemLiveGoodsPutOnBinding
import com.vitabuybuy.tv.databinding.PopupLiveGoodsBinding
import com.vitabuybuy.tv.ui.tencent.liveroom.bean.LiveGoodsBean
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig


class PutOnLiveGoofsPopup(context: Context) : BasePopupWindow(context) {

    private lateinit var mAdapter: LiveGoodsAdapter

    private lateinit var mBinding: PopupLiveGoodsBinding

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_live_goods)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as PopupLiveGoodsBinding
        mBinding.clParentView.setOnTouchListener { _, _ ->
            dismiss()
            true
        }
        mAdapter = LiveGoodsAdapter()
        mBinding.listview.apply {
            adapter = mAdapter
            addItemDecoration(RecycleViewDivider(context, DensityUtil.dip2px(1f)))
            layoutManager = LinearLayoutManager(context)
        }
        mAdapter.onPutOnListener = { it, position ->
            onPutOnListener.invoke(it, position)
            dismiss()
        }
        mAdapter.onRecordingListener = { it, position ->
            onRecordingListener.invoke(it, position)
            dismiss()
        }
    }

    fun initData(goodList: MutableList<LiveGoodsBean>) {
        if (goodList.isEmpty()) {
            mBinding.statusView.showEmpty()
        } else {
            mAdapter.setNewData(goodList)
            mBinding.statusView.showContent()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.TO_BOTTOM)
            .toDismiss()
    }

    class LiveGoodsAdapter : BindRecycAdapter<ItemLiveGoodsPutOnBinding, LiveGoodsBean>() {
        override fun onBindViewHolder(
            mBinding: ItemLiveGoodsPutOnBinding,
            itemData: LiveGoodsBean,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            mBinding.itemData = itemData
            mBinding.btnPutOn.setOnClickListener {
                if (!itemData.isPutOn) {
                    onPutOnListener.invoke(itemData, position)
                }
            }
            mBinding.btnRecording.setOnClickListener {
                onRecordingListener.invoke(
                    itemData,
                    position
                )
            }
        }

        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_live_goods_put_on
        }

        lateinit var onPutOnListener: (backUrl: LiveGoodsBean, position: Int) -> Unit
        lateinit var onRecordingListener: (id: LiveGoodsBean, position: Int) -> Unit
    }

    lateinit var onPutOnListener: (backUrl: LiveGoodsBean, position: Int) -> Unit
    lateinit var onRecordingListener: (id: LiveGoodsBean, position: Int) -> Unit
}